/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package databaseLayer;

import java.sql.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import javax.naming.NamingException;

import userBean.*;

/**
 *
 * @author Rigsby Chi
 */
public class UserLookUp {

    public static void main(String arg[]){
        
        RegisteredCustomer me = (RegisteredCustomer) UserLookUp.getUserIdentity("52584035", "52584035");
        System.out.println("ID: " + me.getId());
        System.out.println("Name: " + me.getName());
        System.out.println("Type: " + me.getUserType());
        System.out.println("Points: " + me.getLoyaltyPoint());
        PurchaseRecord pr[] = me.getPurchaseRecord();
        for (int i = 0; pr != null && i < pr.length; i++) {
            System.out.print("ID: " + pr[i].getRecordID() + "  ");
            System.out.print("Movie: " + pr[i].getMovieName() + "  ");
            System.out.print("Amount: " + pr[i].getAmount() + "  ");
            System.out.print("Time: " + pr[i].getPurchaseTime() + "  ");
            System.out.print("Price: " + pr[i].getTotalPrice() + "  ");
            System.out.print("PUsed: " + pr[i].getUsedLoyaltyPoint() + "  ");
            System.out.print("State: " + pr[i].getState() + "\n\n");
        }
    }
    
    public static int getPoint(String loginName){
        int point=0;
        try {

            Context initCtx = new InitialContext();
            Context envCtx = (Context)initCtx.lookup("java:comp/env");
            DataSource ds = (DataSource)envCtx.lookup("jdbc/Assignment2");
            Connection con = ds.getConnection();

            PreparedStatement pstmt = con.prepareStatement("SELECT points FROM LoginUser WHERE LOGINNAME=?");
            pstmt.setString(1, loginName);

            ResultSet rs = pstmt.executeQuery();
            if (rs != null && rs.next()) {
                point = rs.getInt("points");
            }
            if (rs != null) {
                rs.close(); //close ResultSet object
            }
            if (pstmt != null) {
                pstmt.close(); //close Statement object
            }
            if (con != null) {
                con.close(); //close Connection object 
            }
        } catch (SQLException s) {
            s.printStackTrace();
        } catch (NamingException e) {
            e.printStackTrace();
        }
        return point;
    }

    public static UserIdentity getUserIdentity(String loginName, String password) {
        UserIdentity userObject = null;

        try {

            Context initCtx = new InitialContext();
            Context envCtx = (Context)initCtx.lookup("java:comp/env");
            DataSource ds = (DataSource)envCtx.lookup("jdbc/Assignment2");
            Connection con = ds.getConnection();

            PreparedStatement pstmt = con.prepareStatement("SELECT * FROM LoginUser WHERE LOGINNAME=? AND PASSWORD=?");
            pstmt.setString(1, loginName);
            pstmt.setString(2, password);

            ResultSet rs = pstmt.executeQuery();
            if (rs != null && rs.next()) {
                if (rs.getInt("islogin") == 1) {
                    userObject = new BreakInUser();
                } else if (rs.getInt("USERIDENTITY") == 0) {
                    userObject = new RegisteredCustomer();
                    ((RegisteredCustomer) userObject).setId(loginName);
                    ((RegisteredCustomer) userObject).setName(rs.getString("NAME"));
                    ((RegisteredCustomer) userObject).setLoyaltyPoint(rs.getInt("POINTS"));
                    ((RegisteredCustomer) userObject).setPurchaseRecord(RecordLookUp.getPurchaseRecord(loginName));
                } else if (rs.getInt("USERIDENTITY") == 1) {
                    userObject = new TicketingOfficer();
                    ((TicketingOfficer) userObject).setId(loginName);
                    ((TicketingOfficer) userObject).setName(rs.getString("NAME"));
                } else if (rs.getInt("USERIDENTITY") == 2) {
                    userObject = new TicketingManager();
                    ((TicketingManager) userObject).setId(loginName);
                    ((TicketingManager) userObject).setName(rs.getString("NAME"));
                }
            } else {
                userObject = new Visitor();
            }
            if (rs != null) {
                rs.close(); //close ResultSet object
            }
            if (pstmt != null) {
                pstmt.close(); //close Statement object
            }
            if (con != null) {
                con.close(); //close Connection object 
            }
        } catch (SQLException s) {
            s.printStackTrace();
            userObject = new Visitor();
        } catch (NamingException e) {
            e.printStackTrace();
            userObject = new Visitor();
        }
        return userObject;
    }
}